package com.bz.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bz.dao.MenuDao;
import com.bz.entity.MenuEntity;
import com.bz.model.MenuDto;
import com.bz.model.MenuTree;
import com.bz.service.IMenuService;

@Service
public class MenuServiceImpl implements IMenuService {

    @Autowired
    private MenuDao menuDao;

    @Override
    public List<MenuTree> getMenu() {

        LambdaQueryWrapper<MenuEntity> query = new LambdaQueryWrapper<>();
        query.orderByAsc(MenuEntity::getSort);

        List<MenuEntity> menuDtoList = menuDao.selectList(query);
        List<MenuDto> menuList = convert(menuDtoList);
        return convertTree(menuList);
    }

    public static List<MenuDto> convert(List<MenuEntity> menuList) {
        if (CollectionUtils.isEmpty(menuList)) {
            return null;
        }
        List<MenuDto> editList = new ArrayList<>();

        for (MenuEntity m : menuList) {

            // 没有父节点，为根节点
            if (null == m.getPid()) {
                editList.add(convert(m));
            }
        }

        for (MenuDto m : editList) {
            for (MenuEntity ch : menuList) {
                if (ch.getPid() == m.getId()) {
                    List<MenuDto> clist = m.getMenuList();
                    if (CollectionUtils.isEmpty(clist)) {
                        clist = new ArrayList<>();
                    }
                    clist.add(convert(ch));
                    m.setMenuList(clist);
                }
            }
        }

        for (MenuDto m1 : editList) {
            if (CollectionUtils.isEmpty(m1.getMenuList())) {
                continue;
            }
            for (MenuDto cm : m1.getMenuList()) {
                for (MenuEntity ch : menuList) {
                    if (ch.getPid() == cm.getId()) {
                        List<MenuDto> clist = cm.getMenuList();
                        if (CollectionUtils.isEmpty(clist)) {
                            clist = new ArrayList<>();
                        }
                        clist.add(convert(ch));
                        cm.setMenuList(clist);
                    }
                }
            }
        }

        return editList;
    }

    private static MenuDto convert(MenuEntity data) {
        MenuDto m = new MenuDto();
        m.setId(data.getId());
        m.setIcon(data.getIcon());
        m.setLevel(data.getLevel());
        m.setName(data.getName());
        m.setPid(data.getPid());
        m.setUrl(data.getUrl());
        return m;
    }
    
    /**
     * 转换
     * 
     * @param menuList
     * @return
     */
    public List<MenuTree> convertTree(List<MenuDto> menuList) {
        List<MenuTree> mtlist = new ArrayList<>();
        if (!CollectionUtils.isEmpty(menuList)) {
            for (MenuDto menu : menuList) {
                MenuTree mt = new MenuTree();
                mt.setId(menu.getId());
                mt.setTitle(menu.getName());
                mt.setUrl(menu.getUrl());
                mt.setIcon(menu.getIcon());
                List<MenuDto> clist = menu.getMenuList();
                if (CollectionUtils.isEmpty(clist)) {
                    mtlist.add(mt);
                    continue;
                }
                List<MenuTree> cmtlist = new ArrayList<>();
                for (MenuDto cm : clist) {
                    MenuTree cmt = new MenuTree();
                    cmt.setId(cm.getId());
                    cmt.setTitle(cm.getName());
                    cmt.setUrl(cm.getUrl());
                    cmt.setIcon(cm.getIcon());
                    cmtlist.add(cmt);

                    List<MenuDto> cclist = cm.getMenuList();
                    if (CollectionUtils.isEmpty(cclist)) {
                        continue;
                    }

                    List<MenuTree> ccmtlist = new ArrayList<>();
                    for (MenuDto ccm : cclist) {
                        MenuTree ccmt = new MenuTree();
                        ccmt.setId(ccm.getId());
                        ccmt.setTitle(ccm.getName());
                        ccmtlist.add(ccmt);
                    }
                    cmt.setChildren(ccmtlist);
                }
                mt.setChildren(cmtlist);
                mtlist.add(mt);
            }
        }
        return mtlist;
    }
}
